<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <link rel="stylesheet" href="../css/orderset.css">
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
</head>
<style>
    .usual {
    }

    .fulled {
        color: red;
    }
</style>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>预约管理
            <small>预约设置</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>预约管理</el-breadcrumb-item>
            <el-breadcrumb-item>预约设置</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="box ordersetting">
                <el-card class="box-card">
                    <div class="boxMain">
                        <el-button style="margin-bottom: 20px;margin-right: 20px" type="primary"
                                   @click="downloadTemplate()">模板下载
                        </el-button>
                        <el-upload action="/ordersetting/upload.do"
                                   name="excelFile"
                                   :show-file-list="false"
                                   :on-success="handleSuccess"
                                   :before-upload="beforeUpload">
                            <el-button type="primary">上传文件</el-button>
                        </el-upload>
                    </div>
                    <div>
                        操作说明：请点击"模板下载"按钮获取模板文件，在模板文件中录入预约设置数据后点击"上传文件"按钮上传模板文件。
                    </div>
                </el-card>
                <div class="calendar">
                    <!--Calendar calendar-->
                    <el-calendar ref="settingCalendar" v-model="calendarValue">
                        <template slot="dateCell" slot-scope="{date, data}">
                            <!--date代表当前格子的日期-->
                            {{date.getMonth() + 1}}-{{date.getDate()}}

                            <!-- 日期大于当前日期才有设置按钮 -->
                            <i v-if="date > today" class="el-icon-edit" @click="handleOrderSet(date)"></i>
                            <br>
                            <template v-for="item in leftobj">
                                <template v-if="item.date == date.getDate() && date.getMonth() == item.month">
                                    <template v-if="item.number > item.reservations">
                                        <div class="usual">
                                            <p>可预约{{item.number}}人</p>
                                            <p>已预约{{item.reservations}}人</p>
                                        </div>
                                    </template>
                                    <template v-else>
                                        <div class="fulled">
                                            <p>约满</p>
                                        </div>
                                    </template>
                                </template>
                            </template>

                        </template>
                    </el-calendar>

                </div>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            today: new Date(),//当前日期
            leftobj: [],//用于装载页面显示的月份已经进行预约设置的数据
            calendarValue: '',//日历当前的value
        },
        mounted: function () {
            this.initData(null);
        },
        methods: {
            //预约设置
            handleOrderSet(date) {
                console.log(date);
                this.$prompt('请输入可预约人数', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    // inputPattern字段自己规定匹配模式
                    inputPattern: /(^[1-9]\d*$)/,
                    inputErrorMessage: '请输入正整数'
                }).then(({value}) => {
                    console.log(value);
                    var param = {
                        orderDate: this.formatDate(date.getFullYear(), (date.getMonth() + 1), date.getDate()),
                        number: value
                    }
                    console.log(param);
                    axios.post("/ordersetting/setNumberByDate.do", param)
                        .then((res) => {
                            //提示用户
                            //刷新页面
                            this.initData(date);
                        });

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '取消输入'
                    });
                });
            },
            initData: function (date) {
                if (!date) {
                    //获取日历对象
                    let settingCalendar = this.$refs.settingCalendar;
                    console.log(settingCalendar);
                    //获取当前时间
                    date = settingCalendar.date;
                }

                let formatDate = date.getFullYear() + "-" + (date.getMonth() + 1);
                if ((date.getMonth() + 1) < 10) {
                    formatDate = date.getFullYear() + "-0" + (date.getMonth() + 1);
                }
                axios.post("/ordersetting/findByMonth.do", {
                    date: formatDate
                })
                    .then((res) => {
                        console.log(res);
                        this.leftobj = res.data.data;
                        //提示用户
                        this.$message({
                            type: res.data.flag ? 'success' : 'error',
                            message: res.data.message
                        });
                    });
                // console.log(formatDate);
                // this.leftobj = [
                //     {"date":22,"mouth":0,"number":300,"reservations":300,},
                //     {"date":23,"mouth":0,"number":300,"reservations":10,},
                //     {"date":24,"mouth":0,"number":300,"reservations":15,},
                //     {"date":25,"mouth":0,"number":300,"reservations":100,},
                //     {"date":26,"mouth":0,"number":300,"reservations":150,},
                //     {"date":27,"mouth":0,"number":300,"reservations":1,},
                // ]
            },
            // 返回 类似 2016-01-02 格式的字符串
            formatDate(year, month, day) {
                var y = year;
                var m = month;
                if (m < 10) m = "0" + m;
                var d = day;
                if (d < 10) d = "0" + d;
                return y + "-" + m + "-" + d
            },				//上传之前进行文件格式校验
            beforeUpload(file) {
                const isXLS = file.type === 'application/vnd.ms-excel';
                if (isXLS) {
                    return true;
                }
                const isXLSX = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
                if (isXLSX) {
                    return true;
                }
                this.$message.error('上传文件只能是xls或者xlsx格式!');
                return false;
            },
            //下载模板文件
            downloadTemplate() {
                window.location.href = "../../template/ordersetting_template.xlsx";
                // window.location.href = "http://www.baidu.com";
            },
            //上传成功提示
            handleSuccess(response, file) {
                console.log(response);
                if (response.flag) {
                    this.$message({
                        message: response.message,
                        type: 'success'
                    });
                } else {
                    this.$message.error(response.message);
                }
                // console.log(response, file, fileList);
            },
            //初始化当前页要展示的日期
        },
        watch: {
            //当点击上月 下月 当前月触发
            calendarValue(newVal, oldVal) {
                this.initData(newVal);
            }
        }
    })
</script>
</html>
